package org.jeecg.modules.wlhy.captain.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.captain.entity.HyCaptain;
import org.jeecg.modules.wlhy.captain.entity.HyCaptainVehicle;
import org.jeecg.modules.wlhy.captain.service.IHyCaptainService;
import org.jeecg.modules.wlhy.captain.service.IHyCaptainVehicleService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: hy_captain_vehicle
 * @Author: jeecg-boot
 * @Date:   2024-11-01
 * @Version: V1.0
 */
@Api(tags="hy_captain_vehicle")
@RestController
@RequestMapping("/captain/hyCaptainVehicle")
@Slf4j
public class HyCaptainVehicleController extends JeecgController<HyCaptainVehicle, IHyCaptainVehicleService> {
	@Autowired
	private IHyCaptainVehicleService hyCaptainVehicleService;

	@Autowired
	private UserUtil userUtil;

	@Autowired
	private ISysUserService userService;

	@Autowired
	private IHyCaptainService captainService;

	@Autowired
	private IHyDriverService driverService;

	@Autowired
	private IHyVehicleService vehicleService;

	/**
	 * 分页列表查询
	 *
	 * @param hyCaptainVehicle
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_captain_vehicle-分页列表查询", notes="hy_captain_vehicle-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCaptainVehicle hyCaptainVehicle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCaptainVehicle> queryWrapper = QueryGenerator.initQueryWrapper(hyCaptainVehicle, req.getParameterMap());
		Page<HyCaptainVehicle> page = new Page<HyCaptainVehicle>(pageNo, pageSize);
		IPage<HyCaptainVehicle> pageList = hyCaptainVehicleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param hyCaptainVehicle
	 * @return
	 */
	@AutoLog(value = "hy_captain_vehicle-添加")
	@ApiOperation(value="hy_captain_vehicle-添加", notes="hy_captain_vehicle-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyCaptainVehicle hyCaptainVehicle) {
		HyCaptain captain ;
		if (userUtil.isCaptain()){
			captain = captainService.getInfoByUserId(userUtil.getLoginUser().getId())	;
		}else {
			captain = captainService.getById(hyCaptainVehicle.getCaptainId());
		}
		if (captain == null){
			return Result.error("承运商不存在");
		}
		if (StringUtils.isEmpty(captain.getLoginPhone())){
			return Result.error("承运商手机号不能为空");
		}
		SysUser user = userService.getUserByPhone(captain.getLoginPhone());
		if (user == null){
			return Result.error("承运商手机号未注册用户");
		}
		if (hyCaptainVehicleService.count(new QueryWrapper<HyCaptainVehicle>().eq("captain_id", hyCaptainVehicle.getCaptainId()).eq("vehicle_id",hyCaptainVehicle.getVehicleId())) > 0){
			return Result.error("承运商已绑定该车辆");
		}
		if (hyCaptainVehicleService.count(new QueryWrapper<HyCaptainVehicle>().eq("vehicle_id", hyCaptainVehicle.getVehicleId())) > 0){
			return Result.error("该车辆已被绑定");
		}
		hyCaptainVehicle.setUserId(userService.getUserByPhone(captain.getLoginPhone()).getId());
		hyCaptainVehicle.setCreateBy(userUtil.getLoginUser().getUsername());
		hyCaptainVehicle.setCreateTime(new Date());
		hyCaptainVehicle.setCaptainId(captain.getId());
		hyCaptainVehicleService.save(hyCaptainVehicle);
		HyVehicle hyVehicle = new HyVehicle();
		hyVehicle.setId(hyCaptainVehicle.getVehicleId());
		hyVehicle.setCarrierId(hyCaptainVehicle.getCaptainId());
		hyVehicle.setUpdateBy(userUtil.getLoginUser().getUsername());
		hyVehicle.setUpdateTime(new Date());
		vehicleService.updateById(hyVehicle);
		List<HyDriver> drivers = vehicleService.getDriversByVehicleId(hyCaptainVehicle.getVehicleId());
		for (HyDriver driver : drivers) {
			driver.setCarrierId(hyCaptainVehicle.getCaptainId());
		}
		driverService.updateBatchById(drivers);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param hyCaptainVehicle
	 * @return
	 */
	@AutoLog(value = "hy_captain_vehicle-编辑")
	@ApiOperation(value="hy_captain_vehicle-编辑", notes="hy_captain_vehicle-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyCaptainVehicle hyCaptainVehicle) {
		hyCaptainVehicle.setUserId(userUtil.getLoginUser().getId());
		hyCaptainVehicle.setUpdateBy(userUtil.getLoginUser().getUsername());
		hyCaptainVehicle.setUpdateTime(new Date());
		hyCaptainVehicleService.updateById(hyCaptainVehicle);

		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "hy_captain_vehicle-通过id删除")
	@ApiOperation(value="hy_captain_vehicle-通过id删除", notes="hy_captain_vehicle-通过id删除")
	@PutMapping(value = "/delete")
	public Result<?> delete(@RequestBody JSONObject jsonObject) {
		String vehicleId = jsonObject.getString("id");
		HyCaptainVehicle hyCaptainVehicle = hyCaptainVehicleService.getOne(new QueryWrapper<HyCaptainVehicle>().eq("vehicle_id", vehicleId));
		if(hyCaptainVehicle ==null) {
			return Result.error("未找到对应数据");
		}
		//删除关联车辆数据中的关联表id
		HyVehicle hyVehicle = new HyVehicle();
		hyVehicle.setCarrierId("");
		hyVehicle.setId(hyCaptainVehicle.getVehicleId());
		vehicleService.updateById(hyVehicle);
		//根据车辆的id获取所有的司机id
		List<HyDriver> drivers = vehicleService.getDriversByVehicleId(hyCaptainVehicle.getVehicleId());
		for (HyDriver driver : drivers) {
			driver.setCarrierId("");
		}
		driverService.updateBatchById(drivers);
		//根据车辆id删除关联表数据
		hyCaptainVehicleService.remove(new QueryWrapper<HyCaptainVehicle>().eq("vehicle_id", vehicleId));
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_captain_vehicle-批量删除")
	@ApiOperation(value="hy_captain_vehicle-批量删除", notes="hy_captain_vehicle-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyCaptainVehicleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_captain_vehicle-通过id查询", notes="hy_captain_vehicle-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCaptainVehicle hyCaptainVehicle = hyCaptainVehicleService.getById(id);
		if(hyCaptainVehicle ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCaptainVehicle);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCaptainVehicle
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyCaptainVehicle hyCaptainVehicle) {
        return super.exportXls(request, hyCaptainVehicle, HyCaptainVehicle.class, "hy_captain_vehicle");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyCaptainVehicle.class);
    }

}
